<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /* 手写call */
        // 实现思路：给绑定对象添加属性，调用后删除
        Function.prototype.myCall = function (obj) {
            if (typeof this !== 'function') {
                throw new Error('Type Error')
            }

            let res = null; //调用结果存储
            let args = [...arguments].slice(1); // 获取传入参数
            obj ? obj : window; // 判断是否传入调用对象

            const fnSymbol = Symbol(); // 保证属性不重名
            obj[fnSymbol] = this; // 给对象fn属性赋值要绑定的函数
            res = obj[fnSymbol](...args); // 执行函数

            delete obj[fnSymbol]; // 删除属性

            return res;
        }

        /* 手写apply */
        Function.prototype.myApply = function (obj) {
            if (typeof this !== 'function') {
                throw new Error('Type Error')
            }

            let res = null; //调用结果存储
            obj ? obj : window; // 判断是否传入调用对象

            const fnSymbol = Symbol(); // 保证属性不重名
            obj[fnSymbol] = this; // 给对象fn属性赋值要绑定的函数

            res = obj[fnSymbol](...arguments[1]) // 传入参数

            delete obj[fnSymbol]; // 删除属性

            return res;
        }

        /* 手写bind */
        Function.prototype.myBind = function (obj) {
            if (typeof this !== 'function') {
                throw new Error('Type Error')
            }

            let args1 = [...arguments].slice(1); // 获取第一次传入参数
            const fn = this; // this指向调用的对象

            return function Fn() {
                let args2 = [...arguments]; // 第二次传入的参数
                return fn.apply(
                    // this instanceof Fn ? this : obj,
                    obj,
                    args1.concat(...args2)
                )
            }
        }

        /* 手写new */
        const myNew = (constructor, ...args) => {
            const obj = {}
            obj.__proto__ = constructor.prototype
            const res = constructor.call(obj, ...args)
            return typeof res === 'object' ? res : obj
        }

        /* 手写curry */
        function curry(fn) {
            // your code here
            const preArgs = [...arguments].slice(1) // 和函数一起传入的参数
            const length = fn.length // 原函数需要的参数个数

            return function () {
                const allArgs = [...preArgs, ...arguments]

                if (allArgs.length >= length) {
                    // 达到要求时执行
                    return fn.apply(this, allArgs)
                } else {
                    // 参数没达到要求时接着柯里化
                    return curry.call(this, fn, ...allArgs)
                }
            }
        }

        /* 手写节流throttle */
        function throttle1(func, wait) {
            let lastTime = null
            return function () {
                const nowTime = Date.now()
                if (nowTime - lastTime > delay) {
                    func.apply(this, arguments)
                    lastTime = nowTime
                }
            }
        }
        function throttle2(func, wait) {
            let waiting = false;
            let lastArgs;
            return function () {
                if (!waiting) {
                    waiting = true;
                    func.apply(this, arguments);
                    setTimeout(() => {
                        waiting = false;
                        if (lastArgs) func.apply(this, lastArgs);
                    }, wait);
                } else {
                    lastArgs = [...arguments];
                }
            }
        }

        /* 手写防抖debounce */
        function debounce(func, delay) {
            let timer = null;
            return function () {
                clearTimeout(timer)
                timer = setTimeout(() => {
                    func.apply(this, arguments)
                }, delay)
            }
        }

        /* 手写继承(寄生组合式) */
        function Inherit() {
            function Parent(name, age) {
                this.name = name
                this.age = age
            }
            Parent.prototype.func = function () { console.log('I AM FUNC'); }
            function Child(sex) {
                // 解决原型链继承的属性问题
                Parent.call(this)
                this.sex = sex
            }
            // 解决构造函数继承方法不能复用问题
            Child.prototype = Object.create(Parent.prototype)
            Child.prototype.constructor = Child

            let parent = new Parent('parent', '80')
            let child = new Child('women')
            parent.func()
            console.log(child);
        }

        Inherit();

        /* 手写深拷贝 */
        function clone(target, map = new Map()) {
            if (typeof target === 'object') {
                let cloneTarget = target instanceof Array ? [] : {};
                if (map.get(target)) {
                    return map.get(target)
                }
                map.set(target, cloneTarget)
                for (const key in target) {
                    cloneTarget[key] = clone(target[key], map);
                }
                return cloneTarget;
            } else {
                return target;
            }
        };
        // 无循环引用
        function copy(dest) {
            if (typeof dest === 'object') {
                if (!dest) return dest; // null
                var obj = dest.constructor();  // Object/Array
                for (var key in dest) {
                    obj[key] = copy(dest[key])
                }
                return obj;
            } else {
                return dest;
            }
        }

        /* 手写getUrlParams */
        function getUrlParams(url) {
            const query = url.split('?')[1];
            if (!query) return null;
            const arr = query.split('&');
            const obj = {};
            for (let i = 0; len = arr.length, i < len; i++) {
                const map = arr[i].split('=');
                obj[map[0]] = map[1];
                console.log(obj);
            }
            return obj;
        }

        /* reduce 实现 map */
        Array.prototype.myMap = function (callback) {
            // reduce参数 fun(preValue, currValue, currIndex, arr), initValue
            // map参数 fun(currValue, currindex, arr)
            if (typeof callback === 'function') {
                return this.reduce((prev, curr, index, arr) => {
                    prev.push(callback(curr, index, arr))
                    return prev;
                }, [])
            }
        }


        const obj = { a: 1, b: 2, c: 3 }
        const arr = [];
        const obj2 = Object.assign({}, obj)
        console.log((obj == obj2));

        

    </script>
</body>

</html>